package com.tuyenmonkey.mkloader.type;

import com.tuyenmonkey.mkloader.exception.InvalidNumberOfPulseException;
import com.tuyenmonkey.mkloader.model.Line;
import com.tuyenmonkey.mkloader.util.AnimatorValueUtil;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Point;

import java.math.BigDecimal;


public class Pulse extends LoaderView {
  private Line[] lines;
  private int numberOfLines;
  private float lineWidth;
  private float lineDistance;
  private float[] scaleY;
  private AnimatorValueUtil animatorValueUtil;

  public Pulse(int numberOfLines) throws InvalidNumberOfPulseException {
    if (numberOfLines < 3 || numberOfLines > 5) {
      throw new InvalidNumberOfPulseException();
    }

    this.numberOfLines = numberOfLines;
    lines = new Line[numberOfLines];
    scaleY = new float[numberOfLines];
  }

  @Override public void initializeObjects() {
    lineWidth = (float) width / (2 * numberOfLines);
    lineDistance = lineWidth / 4f;
    float firstX = (new BigDecimal( (new BigDecimal(lineWidth).multiply(new BigDecimal(numberOfLines))
            .add(new BigDecimal(lineWidth).multiply(new BigDecimal(numberOfLines).subtract(new BigDecimal(1)))).floatValue()
            ))).divide(new BigDecimal(2)).add(new BigDecimal(lineWidth).divide(new BigDecimal(2))) .floatValue();
           // .add(new BigDecimal(lineWidth))
    for (int i = 0; i < numberOfLines; i++) {
      lines[i] = new Line();
      lines[i].setColor(color);
      lines[i].setWidth(lineWidth);
      lines[i].setPoint1(new Point(firstX, new BigDecimal(center.getPointY()).subtract(new BigDecimal(height).divide(new BigDecimal(4))).floatValue()));

      lines[i].setPoint2(new Point(firstX, new BigDecimal(center.getPointY()).add(new BigDecimal(height).divide(new BigDecimal(4))).floatValue()));

    }
  }

  @Override public void setUpAnimation() {
    for (int i = 0; i < numberOfLines; i++) {
      final int index = i;
      AnimatorValue scaleAnimator = new AnimatorValue();
      scaleAnimator.setDuration(1000);
      scaleAnimator.setDelay(i * 120);
      scaleAnimator.setLoopedCount(AnimatorValue.INFINITE);
      animatorValueUtil = new AnimatorValueUtil();
      animatorValueUtil.ofFloat(1f, 1.5f, 1f);
      scaleAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
        @Override
        public void onUpdate(AnimatorValue animatorValue, float v) {
          scaleY[index] = animatorValueUtil.getValue(v);
          if (invalidateListener != null) {
            invalidateListener.reDraw();

          }
        }
      });

      scaleAnimator.start();
    }
  }

  @Override public void draw(Canvas canvas) {
    for (int i = 0; i < numberOfLines; i++) {
      canvas.save();
      canvas.translate(i * (new BigDecimal(lineWidth).add(new BigDecimal(lineDistance)).floatValue()), 0);

      canvas.scale(1, scaleY[i], lines[i].getPoint1().getPointX(), center.getPointY());
      lines[i].draw(canvas);
      canvas.restore();
    }
  }
}
